{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x = tf.Variable(1.0,dtype=tf.float32)\n",
    "\n",
    "\n",
    "@tf.function(input_signature=[tf.TensorSpec(shape=[],dtype=tf.float32)])\n",
    "def add_print(a):\n",
    "    x.assign_add(a)\n",
    "    tf.print(x)\n",
    "    return x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\r\n"
     ]
    },
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=4.0>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add_print(tf.constant(3.0))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "class DemoModule(tf.Module):\n",
    "    def __init__(self,init_value = tf.constant(0.0),name=None):\n",
    "        super(DemoModule,self).__init__(name=name)\n",
    "        with self.name_scope:\n",
    "            self.x = tf.Variable(init_value,dtype=tf.float32,trainable=True)\n",
    "\n",
    "    @tf.function(input_signature=[tf.TensorSpec(shape=[],dtype=tf.float32)])\n",
    "    def add_print(self,a):\n",
    "        with self.name_scope:\n",
    "            self.x.assign_add(a)\n",
    "            tf.print(self.x)\n",
    "            return self.x"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\r\n"
     ]
    }
   ],
   "source": [
    "demo = DemoModule(init_value=tf.constant(1.0))\n",
    "result = demo.add_print(tf.constant(5.0))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(<tf.Variable 'demo_module/Variable:0' shape=() dtype=float32, numpy=6.0>,)\n",
      "(<tf.Variable 'demo_module/Variable:0' shape=() dtype=float32, numpy=6.0>,)\n"
     ]
    }
   ],
   "source": [
    "print(demo.variables)\n",
    "print(demo.trainable_variables)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "()"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "demo.submodules"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\python\\ops\\resource_variable_ops.py:1817: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "If using Keras pass *_constraint arguments to layers.\n",
      "INFO:tensorflow:Assets written to: ./model/demo/1\\assets\n"
     ]
    }
   ],
   "source": [
    "tf.saved_model.save(demo,'./model/demo/1',signatures={\"serving_default\":demo.add_print})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\r\n"
     ]
    },
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=11.0>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "demo2 = tf.saved_model.load('./model/demo/1')\n",
    "demo2.add_print(tf.constant(5.0))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\r\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "\n",
    "stamp = datetime.datetime.now().strftime(\"%Y%m%d-%h%M%S\")\n",
    "logdir = '././model/demomodule/%s' % stamp\n",
    "writer = tf.summary.create_file_writer(logdir)\n",
    "\n",
    "tf.summary.trace_on(graph=True,profiler=True)\n",
    "\n",
    "demo = DemoModule(init_value=tf.constant(0.0))\n",
    "result = demo.add_print(tf.constant(5.0))\n",
    "\n",
    "\n",
    "with writer.as_default():\n",
    "    tf.summary.trace_export(\n",
    "        name='demomodule',\n",
    "        step = 0,\n",
    "        profiler_outdir=logdir\n",
    "    )\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "mymodule = tf.Module()\n",
    "mymodule.x = tf.Variable(0.0)\n",
    "\n",
    "@tf.function(input_signature=[tf.TensorSpec(shape=[],dtype=tf.float32)])\n",
    "def add_print(a):\n",
    "    mymodule.x.assign_add(a)\n",
    "    tf.print(mymodule.x)\n",
    "    return mymodule.x\n",
    "\n",
    "mymodule.add_print = add_print\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\r\n"
     ]
    },
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mymodule.add_print(tf.constant(1.0)).numpy()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0>,)\n"
     ]
    }
   ],
   "source": [
    "print(mymodule.variables)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Assets written to: ./data/mymodule\\assets\n",
      "6\r\n"
     ]
    },
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=float32, numpy=6.0>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.saved_model.save(mymodule,'./data/mymodule',\n",
    "signatures={\"serving_default\":mymodule.add_print})\n",
    "\n",
    "mymodule2 = tf.saved_model.load('./data/mymodule')\n",
    "mymodule2.add_print(tf.constant(5.0))\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras import models,layers,losses,metrics\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(issubclass(tf.keras.Model,tf.Module))\n",
    "print(issubclass(tf.keras.layers.Layer,tf.Module))\n",
    "print(issubclass(tf.keras.Model,tf.keras.layers.Layer))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 4)                 44        \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 2)                 10        \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 1)                 3         \n",
      "=================================================================\n",
      "Total params: 57\n",
      "Trainable params: 57\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "tf.keras.backend.clear_session()\n",
    "\n",
    "model = models.Sequential()\n",
    "\n",
    "model.add(layers.Dense(4,input_shape=(10,)))\n",
    "model.add(layers.Dense(2))\n",
    "model.add(layers.Dense(1))\n",
    "model.summary()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "[<tf.Variable 'dense/kernel:0' shape=(10, 4) dtype=float32, numpy=\n array([[ 0.2801702 , -0.25442347,  0.27659625, -0.47308004],\n        [-0.4907772 , -0.6534623 , -0.34517008, -0.40074638],\n        [ 0.06092381,  0.398157  ,  0.2672059 ,  0.19566077],\n        [ 0.24416673, -0.38805178, -0.5945067 , -0.11677307],\n        [-0.08150172,  0.521732  , -0.05088121,  0.48491108],\n        [ 0.5648309 ,  0.5361283 ,  0.33330166, -0.09390461],\n        [-0.2412447 , -0.0315361 ,  0.34155756, -0.04307759],\n        [-0.47984397,  0.4660461 , -0.23235586,  0.36120057],\n        [-0.13798642,  0.4896562 ,  0.4209305 ,  0.08264709],\n        [ 0.521883  ,  0.06473029, -0.03931022,  0.483418  ]],\n       dtype=float32)>,\n <tf.Variable 'dense/bias:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>,\n <tf.Variable 'dense_1/kernel:0' shape=(4, 2) dtype=float32, numpy=\n array([[ 0.35215855,  0.8151436 ],\n        [-0.9453914 , -0.45983815],\n        [ 0.3633306 ,  0.42834067],\n        [-0.9173231 , -0.11284184]], dtype=float32)>,\n <tf.Variable 'dense_1/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>,\n <tf.Variable 'dense_2/kernel:0' shape=(2, 1) dtype=float32, numpy=\n array([[ 0.40068102],\n        [-0.41934127]], dtype=float32)>,\n <tf.Variable 'dense_2/bias:0' shape=(1,) dtype=float32, numpy=array([0.], dtype=float32)>]"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.variables"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "[<tf.Variable 'dense_1/kernel:0' shape=(4, 2) dtype=float32, numpy=\n array([[ 0.35215855,  0.8151436 ],\n        [-0.9453914 , -0.45983815],\n        [ 0.3633306 ,  0.42834067],\n        [-0.9173231 , -0.11284184]], dtype=float32)>,\n <tf.Variable 'dense_1/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>,\n <tf.Variable 'dense_2/kernel:0' shape=(2, 1) dtype=float32, numpy=\n array([[ 0.40068102],\n        [-0.41934127]], dtype=float32)>,\n <tf.Variable 'dense_2/bias:0' shape=(1,) dtype=float32, numpy=array([0.], dtype=float32)>]"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.layers[0].trainable = False\n",
    "model.trainable_variables"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "(<tensorflow.python.keras.engine.input_layer.InputLayer at 0x15c5dfa73c8>,\n <tensorflow.python.keras.layers.core.Dense at 0x15c5e337c08>,\n <tensorflow.python.keras.layers.core.Dense at 0x15c5de38488>,\n <tensorflow.python.keras.layers.core.Dense at 0x15c5dd699c8>)"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.submodules"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "[<tensorflow.python.keras.layers.core.Dense at 0x15c5e337c08>,\n <tensorflow.python.keras.layers.core.Dense at 0x15c5de38488>,\n <tensorflow.python.keras.layers.core.Dense at 0x15c5dd699c8>]"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.layers"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequential\n",
      "<bound method Layer._name_scope of <tensorflow.python.keras.engine.sequential.Sequential object at 0x0000015C5DF1A9C8>>\n"
     ]
    }
   ],
   "source": [
    "print(model.name)\n",
    "print(model.name_scope)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}